package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.ann.PermissionName;
import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.List;
import java.util.Map;


@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private ApplicationContext ctx;


    @Override
    public int insert(Permission entity) {
        permissionMapper.insert(entity);


        return 0;
    }

    @Override
    public List<String> selectAllName() {

        return permissionMapper.selectAllName();

    }

    @Override
    public List<Permission> selectAll() {
        //查询所有的权限
        return permissionMapper.selectAll();

    }

    @Override
    public PageResult query(QueryObject qo) {
        //查询总记录数
        int total = permissionMapper.queryForCount();
        /*总页数为0的话,就返回空白页面*/
        if (total == 0) {
            return PageResult.empty_page;
        }
        //查询数据
        List rows = permissionMapper.queryForList(qo);
        return new PageResult(total, rows);
    }

    @Override
    //权限的加载
    public void reload() {
        //1.获取到所有的controller对象
        //查询数据库中所有的权限
        List<String> allName = permissionMapper.selectAllName();
        //2.遍历所有的方法
        //封装了没有被代理的原生的controller中的handler对象
        RequestMappingHandlerMapping requestMappingHandlerMapping = ctx.getBean(RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
        for (HandlerMethod met : handlerMethods.values()) {
            PermissionName permissionName = met.getMethodAnnotation(PermissionName.class);
            if (permissionName != null) {
                //贴有注解的话得到里面的值
                String name = permissionName.value();
                //判断权限时候已经存在数据库中去了
                if (!allName.contains(name)) {
                    RequiresPermissions permissions = met.getMethodAnnotation(RequiresPermissions.class);
                    String[] resources = permissions.value();//得到所有的权限表达式
                    //把权限表达式进行拼接
                    String resource = StringUtils.join(resources, ",");
                    Permission permission = new Permission();
                    permission.setName(name);
                    permission.setResource(resource);
                    permissionMapper.insert(permission);
                }
            }
        }
    }

    @Override
    public List<Permission> queryByRoleId(Long id) {
        List<Permission> permissions = permissionMapper.queryByRoleId(id);
        return permissions;
    }

    @Override
    public void insertPermissionsId(Role role) {
        Long roleId = role.getId();
        //删除
         permissionMapper.deleteRelation(roleId);
        //插入中间表
       if(role != null){
           List<Permission> permissions = role.getPermissions();
           for (Permission permission : permissions) {
               permissionMapper.insertRelationPermission(role.getId(),permission.getId());
           }
       }

    }

    @Override
    public List<Long> selectPermissionByRoleId(Long roleId) {
        return permissionMapper.selectPermissionByRoleId(roleId);
    }

    public List<Long> listIdsByOperatorId(Long operatorId) {
        return permissionMapper.listIdsByOperatorId(operatorId);
    }

}
